package com.yunkaweilai.android.activity.web;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.JavascriptInterface;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import com.github.lazylibrary.util.StringUtils;
import com.yunkaweilai.android.R;
import com.yunkaweilai.android.activity.login.LoginActivity;
import com.yunkaweilai.android.base.BaseActivity;
import com.yunkaweilai.android.base.BaseApplication;
import com.yunkaweilai.android.model.AppRuleModel;
import com.yunkaweilai.android.utils.TitleBuilder;
import com.yunkaweilai.android.utils.Utils;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * web界面
 * Created by Administrator on 2017/8/3.
 */

public class WebActivityWithRufund extends BaseActivity {


    @BindView(R.id.titlebar_tv)
    TextView titlebarTv;
    @BindView(R.id.id_webView)
    WebView idWebView;
    public static final String WEB_URL = "WEB_URL";
    public static final String WEB_URL2 = "WEB_URL2";
    private AppRuleModel.DataBean.InfoBean appRuleModel;
    WebSettings mWebSettings;
    private String url = "";
    private String url2 = "";

    public static void actionStart(Context context, String url, String url2) {
        Intent intent = new Intent(context, WebActivityWithRufund.class);
        intent.putExtra(WEB_URL, url);
        intent.putExtra(WEB_URL2, url2);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web);
        ButterKnife.bind(this);
        appRuleModel = BaseApplication.getAppRuleModel();
        TitleBuilder titleBuilder = new TitleBuilder(mAc);
        titleBuilder.setTitleText("")
                .setLeftImage(R.mipmap.ic_go_back)
                .setLeftOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (idWebView.canGoBack()) {
                            idWebView.goBack();//返回上一页面
                        } else {
                            finish();
                        }
                    }
                });
        if (appRuleModel.isIs_super() || appRuleModel.getApp_rule().get("13")) {
            titleBuilder.setRightText("退款").setRightOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (appRuleModel.isIs_super() || appRuleModel.getApp_rule().get("13")) {
                        WebActivity.actionStart(mAc, url2);
                    }
                }
            });
        }

        url = getIntent().getStringExtra(WEB_URL);
        url2 = getIntent().getStringExtra(WEB_URL2);
        if (StringUtils.isEmpty(url)) {
            url = "http://m.hfwjx.com";
        }
        mWebSettings = idWebView.getSettings();
        mWebSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
        mWebSettings.setJavaScriptEnabled(true);
        idWebView.addJavascriptInterface(new JSInterface(), "Order");
        idWebView.loadUrl(url);

        //设置WebChromeClient类
        idWebView.setWebChromeClient(new WebChromeClient() {
            //获取网站标题
            @Override
            public void onReceivedTitle(WebView view, String title) {
//                titlebarTv.setText(title);
//                titlebarTv.setVisibility(View.VISIBLE);
            }

            //获取加载进度
            @Override
            public void onProgressChanged(WebView view, int newProgress) {

            }
        });

        //设置不用系统浏览器打开,直接显示在当前Webview
        idWebView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });

        //设置WebViewClient类
        idWebView.setWebViewClient(new WebViewClient() {
            //设置加载前的函数
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                titlebarTv.setVisibility(View.GONE);
            }

            //设置结束加载函数
            @Override
            public void onPageFinished(WebView view, String url) {
                showLog(view.getTitle() + "");
                titlebarTv.setVisibility(View.VISIBLE);
                titlebarTv.setText(view.getTitle() + "");
            }
        });


    }


    //点击返回上一页面而不是退出浏览器
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && idWebView.canGoBack()) {
            idWebView.goBack();
            return true;
        }

        return super.onKeyDown(keyCode, event);
    }

    //销毁Webview
    @Override
    protected void onDestroy() {
        if (idWebView != null) {
            idWebView.loadDataWithBaseURL(null, "", "text/html", "utf-8", null);
            idWebView.clearHistory();
            ((ViewGroup) idWebView.getParent()).removeView(idWebView);
            idWebView.destroy();
            idWebView = null;
        }
        super.onDestroy();
    }


    private class JSInterface {
        @JavascriptInterface
        public void OrderInfo(String tid) {//复制订单号
            if (!StringUtils.isEmpty(tid)) {
                Utils.copy2clipboard(mAc, tid);
                showToast("复制成功");
            }
        }

        @JavascriptInterface
        public void OrderRefund() {//退款成功
            showToast("退款成功");
            finish();
        }

        @JavascriptInterface
        public void GotoLogin() {//退款成功
            showToast("登录失效");
            intent2Activity(LoginActivity.class);
        }
    }

}
